using UnityEngine;
using System.Collections;
using System.Threading;

public class PlayerMechanic : DrawableGameObject {
	
	private PlayerState playerState = PlayerState.Normal;
	
	public GameObject playerGo;
	
	public Parallax parallax_bg;
	public Parallax parallax_ground;
	
	public ParallaxMode parallaxMode;
		
	public PowerUpsManager laserPU;
	public PowerUpsManager boostPU;
	public PowerUpsManager anubisPU;
	
	public KeyCode laserKeyCode;
	public KeyCode boostKeyCode;
	public KeyCode anubisKeyCode;
	
	public static float gameVel = 1;
			
	// ------------------- Normal Mode -------------------------

	public float xVel;
	public float yVel;
	
	private float currentVelX;
	private float currentVelY;
	
	public float maxAltitude;
	public float minAltitude;
	
	public float maxLongitude;
	public float minLongitude;
	
	// ------------------- Boost Mode --------------------------
	
	public BoostInfo boostInfo;
	
	// ------------------- Scary Dog Mode --------------------------
	
	public PowerUp_ScaryDogInfo scaryDogInfo;

	// ------------------- Lazer Mode --------------------------

	public PowerUp_LaserInfo lazerInfo;

    private ScoreManager score;
    private ScoreManager coin;
    public GameObject fadeOut;

	public PlayerState getPlayerState()
	{
		return playerState;
	}
	
	void Awake()
	{
		this.playerGo = this.gameObject;
		this.tag = Tags.PLAYER;

		this.scaryDogInfo.Awake();
		this.boostInfo.Awake();
		this.lazerInfo.Awake();
		
		this.parallaxMode = ParallaxMode.Normal;
	}
	
	
	public override void Start() 
	{
        base.Start();

		this.boostInfo.Start();
		this.scaryDogInfo.Start();
		this.lazerInfo.Start();

        score = GameObject.FindGameObjectWithTag(Tags.SCORE_DISTANCE).GetComponent<ScoreManager>();
        coin = GameObject.FindGameObjectWithTag(Tags.SCORE_COIN).GetComponent<ScoreManager>();
	}
	
	void Update () 
    {
		this.parallax_bg.SetParallax(parallaxMode);
		this.parallax_ground.SetParallax(parallaxMode);

        if(playerState != PlayerState.DyingMode)
            score.AddScore_Continuous();
        else
        {
            RecordManager.setLastMeterRecord(score.GetScore());
            RecordManager.setLastScoreRecord(coin.GetScore());

            RecordManager.HandleMatchScore(score.GetScore(), coin.GetScore());
            CatManager.Stop();
        }
		
		if(this.playerState == PlayerState.Normal)
		{
			if(Input.GetKeyDown(this.laserKeyCode) && PowerUpsManager.isPrepared)
			{
				this.laserPU.Use();
                //this.ChangeMode(PlayerState.LaserMode);
			}
            if(Input.GetKeyDown(this.boostKeyCode) && PowerUpsManager.isPrepared)
			{
				this.boostPU.Use();
				//this.ChangeMode(PlayerState.BoostMode);
			}
            if(Input.GetKeyDown(this.anubisKeyCode) && PowerUpsManager.isPrepared)
			{
				this.anubisPU.Use();
				//this.ChangeMode(PlayerState.ScaryDogMode);
			}
		}
		
		
		switch(playerState){
		
		
			case PlayerState.Normal: 	
			
			this.currentVelX = Input.GetAxis("Horizontal") * this.xVel;
			this.currentVelY = Input.GetAxis("Vertical") * this.yVel;
			
			this.currentVelX *= Time.deltaTime;
			this.currentVelY *= Time.deltaTime;
			
			this.playerGo.transform.Translate(new Vector3(this.currentVelX, this.currentVelY, 0));
			
			
			Vector3 tmpPosition = new Vector3(playerGo.transform.position.x, playerGo.transform.position.y, playerGo.transform.position.z);
			
			if(this.playerGo.transform.position.y > this.maxAltitude){
				tmpPosition.y = maxAltitude;
			}
			
			if(this.playerGo.transform.position.y < this.minAltitude){
				tmpPosition.y = minAltitude;
			}
			
			if(this.playerGo.transform.position.x > this.maxLongitude)
			{
				tmpPosition.x = maxLongitude;
			}
			
			if(this.playerGo.transform.position.x < this.minLongitude)
			{
				tmpPosition.x = minLongitude;
			}
			
			this.playerGo.transform.position = tmpPosition;
			
			
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.BoostMode:
			
			this.boostInfo.Update();
			
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.LaserMode:
		
			this.lazerInfo.Update();
		
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.ScaryDogMode:
			
			this.scaryDogInfo.Update();
			
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.DyingMode:
			
			if(transform.position.y > minAltitude - 3)
			{
				transform.Translate(0,-8 * Time.deltaTime,0);
			}
			
		break;
			
		}		
	}

	public void ChangeMode(PlayerState playerState)
	{
		this.playerState = playerState;
		
		switch(this.playerState)
		{
		case PlayerState.Normal: 	
			this.parallaxMode = ParallaxMode.Normal;
			Debug.Log("----- Normal Mode -----");
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.BoostMode:
			this.parallaxMode = ParallaxMode.Boost;
			AudioPlayer.Play(AudioPlayer.PLAYER_BOOST);
			gameVel = this.boostInfo.boostVel;
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.LaserMode:
			AudioPlayer.Play(AudioPlayer.PLAYER_LASER);
			
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.ScaryDogMode:
			AudioPlayer.Play(AudioPlayer.PLAYER_WOOF);
			
		break;
		// ------------------------------------------------------------------------------
		case PlayerState.DyingMode:
			this.parallaxMode = ParallaxMode.Dying;
		break;
			
		}
	}
	
	public void GameOver()
	{
		ChangeMode(PlayerState.DyingMode);
        fadeOut.SetActive(true);
        StartCoroutine("ChangeScene");
	}

    private IEnumerator ChangeScene()
    {
        yield return new WaitForSeconds(1);
        Application.LoadLevel("Pos_Game");
    }
}


public enum PlayerState
{
	Normal, BoostMode, ScaryDogMode, LaserMode, DyingMode
}
